#include "graphics.h"
#include "debug.h"

static sprite_list_node_t **sprite_display_lists;

void init_graphics(){
    int i;

#ifdef LINUX
    init_linux_graphics();
#endif
    sprite_display_lists = (sprite_list_node_t**)malloc(sizeof(sprite_list_node_t*) * NUM_SPRITE_LISTS);
    if(!sprite_display_lists){
        dbg_msga("init_graphics: %s\n", "could not malloc sprite display lists.");
        exit(1);
    }
    for(i = 0; i < NUM_SPRITE_LISTS; i++){
        sprite_display_lists[i] = create_sprite_list();
    }
}

void close_graphics(){
    int i;

    for(i = 0; i < NUM_SPRITE_LISTS; i++){
        free_sprite_list(&(sprite_display_lists[i]));
    }
    free(sprite_display_lists);
}

sprite_list_node_t* add_sprite(sprite_t *spr, const int level){
    if(level < 0 || level > NUM_SPRITE_LISTS){
        dbg_msga("Request to add sprite to level %d, but max level is"
                "%d\n", level, NUM_SPRITE_LISTS);
        exit(1);
    }
    sprite_list_node_t* ret = sprite_list_add(sprite_display_lists[level], spr);
    if(ret && ret->prev == NULL){
        sprite_display_lists[level] = ret;
    }
    return ret;
}

void remove_sprite(sprite_list_node_t* spr, const int level){
    if(level < 0 || level > NUM_SPRITE_LISTS){
        dbg_msga("Request to delete sprite at level %d, but max level is"
                "%d\n", level, NUM_SPRITE_LISTS);
        exit(1);
    }

    sprite_list_node_t* ret = sprite_list_remove(sprite_display_lists[level]);
    if(ret && ret->prev == NULL){
        sprite_display_lists[level] = ret;
    }
}

sprite_list_node_t** get_sprite_lists(){
    return sprite_display_lists;
}
